package com.cqq.print.base.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alibaba.fastjson.JSONObject;
import com.cqq.common.core.base.BaseController;
import com.cqq.print.base.service.PBookCategoryService;
import com.cqq.print.base.service.bean.PBookCategoryBean;
import com.cqq.pro.util.ResponseUtil;

/**
 * 2015/06/25
 *
 * @author chenqq
 */
@Controller(value = "pBookCategoryController")
@RequestMapping(value = { "bookCategory" }, method = {
		RequestMethod.POST,
		RequestMethod.GET })
public class PBookCategoryController extends BaseController{

	@Autowired
	private PBookCategoryService pBookCategoryService;
	
	@RequestMapping("saveBookCateGory")
	public void saveBookCateGory(PBookCategoryBean categoryBean, HttpServletRequest request,HttpServletResponse response) {
		JSONObject json = new JSONObject();
		try{
			List<PBookCategoryBean> list = pBookCategoryService.findByName(categoryBean.getBookCateName());
			if(list==null||list.size()<=0){
				int pBId = pBookCategoryService.insertBookCateGory(categoryBean);
				if(pBId>0){
					json.put("result", "success");
					json.put("pBId", pBId);
				}else{
					json.put("result", "faild");
					json.put("msg", "图书类型对象为空，请重新添加！");
				}
			}else{
				json.put("msg", "改图书类型名已存在，请重新添加！");
			}
		}catch(Exception e){
			json.put("msg", "程序异常，请联系运维人员！");
			log.error(e.getMessage());
		}
		
		ResponseUtil.outJsonString(response, json);
	}
	
}
